package com.longtop.framework.sys.user.web;

/**
 * 用户管理_编辑Action类。
 * 
 * @author 胡砥峰
 * @version 1.0
 * 
 */

import java.util.HashMap;
import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.sys.role.service.RoleService;
import com.longtop.framework.sys.user.model.TSysUser;
import com.longtop.framework.sys.user.service.UserService;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.SpringBeanFactory;
import com.longtop.framework.util.StringUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;

public class UserEditAction extends BaseAction {

	private final static String DATASET_USER = "Dataset_User";
	private final static String DATASET_ROLE = "Dataset_Role";

	/**
	 * 初始化页面
	 * 
	 * @param context
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {
		// 初始化数据
		Dataset userDataset = context.getDataset(DATASET_USER);
		if (!WebUtil.validateDatasetExist(this, userDataset, DATASET_USER)) {
			return;
		}

		String userId = context.getRequest().getParameter("userId");
		// 判断是新增 or 编辑
		if (!StringUtil.isEmpty(userId)) {
			// 调用Spring的业务逻辑类
			UserService userService = getBean(UserService.BEAN_ID);
			HashMap hashMap = new HashMap();
			hashMap.put("id", userId);
			userService.queryForPageByIbatis("sysUserSql.selectUserList", null, hashMap, userDataset, false);
		}

		// 加载角色
		addRoleDataDropDown(context);
	}

	/**
	 * 保存
	 * 
	 * @param context
	 */
	public void saveUser(IActionContext context) {
		Dataset dataset = context.getDataset(DATASET_USER);
		if (!WebUtil.validateDatasetChange(this, dataset, DATASET_USER)) {
			return;
		}

		SessionObject sessionObject = this.getSessionObject(context); // 当前SessionObject对象
		UserService userService = getBean(UserService.BEAN_ID);

		try {
			// 把dataset转成要删除的主键列表、新增bean及更新bean列表
			Map objectList = WebUtil.datasetToObjectList(dataset, "userId", TSysUser.class);
			Integer[] count = userService.saveUser(objectList, sessionObject);

			this.hintInfoMessage(WebUtil.getSubmitSuccessInfo(count[0].intValue(), count[1].intValue(), count[2].intValue()));
		} catch (Exception e) {
			// 在界面显示错误信息
			this.hintErrorMessage(e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 加载角色信息
	 * 
	 * @param context
	 */
	private void addRoleDataDropDown(IInitContext context) {
		Dataset dataset = context.getDataset(DATASET_ROLE);
		if (!WebUtil.validateDatasetExist(this, dataset, DATASET_ROLE)) {
			return;
		}

		RoleService roleService = SpringBeanFactory.getBean(RoleService.BEAN_ID);
		// 设置查询条件列表
		QueryConditions queryConditions = WebUtil.createQueryConditions();

		// 只显示支行不同员工的权限：可自行增加
		String filter = " roleId in ('zhihang_ptyg')";

		QueryResult queryResult = roleService.loadRole(queryConditions, filter);

		try {
			BeanUtil.queryResultToDataset(queryResult, dataset, false, true);
		} catch (Exception e) {
			e.printStackTrace();
			hintErrorMessage(e.getMessage());
		}
	}

	/**
	 * 根据选择的用户类型查找MAX用户ID
	 * 
	 * @param context
	 */
	public void queryMaxUserId(IActionContext context) {
		String userOrgId = this.getParameter(context, "userOrgId");

		UserService userService = getBean(UserService.BEAN_ID);
		String resultUserId = userService.getMaxUserId(userOrgId);

		// 返回获得的MAXuserID到页面
		context.addCommandParameter("resultUserId", resultUserId);
	}
}
